using System.Collections.Generic;
using UnityEngine;

namespace Gameplay.PVE.Skill
{
    public class DashMover : SkillInitiativeMover
    {
        private float startDashTime;
        private float attackStartTime;
        private float duration;
        private int state;
        private List<bool> bulletStates = new List<bool>();
        public override void Start(Vector3 startPosition,Vector3 endPosition)
        {
            base.Start(startPosition,endPosition);
            state = 0;
            startDashTime = TimeManager.logicTime;
            duration = Vector3.Distance(startPosition, endPosition) / unit.Data.moveSpeed;
            bulletStates.Clear();
            for (int i = 0; i < skill.config.bullets.Count; i++)
            {
                bulletStates.Add(false);
            }
        }

        public override void Update()
        {
            base.Update();
            if (state == 0)
            {
                float percent = (TimeManager.logicTime - startDashTime) / duration;
                if (percent > 1)
                {
                    percent = 1;
                    attackStartTime = TimeManager.logicTime;
                    unit.Play("AttackStart");
                }
                unit.Data.position = startPosition + (endPosition - startPosition) * percent;
            }
            else if (state == 1)
            {
                for (int i = 0; i < skill.config.bullets.Count; i++)
                {
                    if (TimeManager.logicTime - attackStartTime >= skill.config.bullets[i].x / 30f && !bulletStates[i])
                    {
                        bulletStates[i] = true;
                        
                    }
                }
            }
        }

        public override void End()
        {
            //unit.Data.position = startPosition;
            unit.Data.attackPosition = unit.Data.position;
            base.End();
        }
    }
}